package io.gitee.thghh.generate.core.model;

import org.mapstruct.AfterMapping;
import org.mapstruct.BeforeMapping;
import org.mapstruct.MappingTarget;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 通用的Mapstruct接口
 *
 * @param <S> source type
 * @param <T> target type
 */
public interface Mapstruct<S, T> {

    /**
     * 类型转换
     *
     * @param s source {@link S}
     * @return target {@link T}
     */
    T mapstruct(S s);

    /**
     * 反向类型转换
     *
     * @param t target {@link T}
     * @return s source {@link S}
     */
    S mapstructReverse(T t);

    /**
     * S2T转换后逻辑
     *
     * @param source {@link S}
     * @param target {@link T}
     */
    @AfterMapping
    default void afterS2T(S source, @MappingTarget T target) {
    }

    /**
     * S2T转换前逻辑
     *
     * @param source {@link S}
     * @param target {@link T}
     */
    @BeforeMapping
    default void beforeS2T(S source, @MappingTarget T target) {
    }

    /**
     * T2S转换后逻辑
     *
     * @param source {@link S}
     * @param target {@link T}
     */
    @AfterMapping
    default void afterT2S(T source, @MappingTarget S target) {
    }

    /**
     * T2S转换前逻辑
     *
     * @param source {@link S}
     * @param target {@link T}
     */
    @BeforeMapping
    default void beforeT2S(T source, @MappingTarget S target) {
    }


    /**
     * 批量正向转换
     *
     * @param s {@link S}
     * @return List {@link T}
     */
    default List<T> batchMapstruct(List<S> s) {
        if (null == s) {
            return null;
        }
        return s.stream().map(this::mapstruct).collect(Collectors.toList());
    }

    /**
     * 批量逆向转换
     *
     * @param t {@link T}
     * @return List {@link S}
     */
    default List<S> batchMapstructReverse(List<T> t) {
        if (null == t) {
            return null;
        }
        return t.stream().map(this::mapstructReverse).collect(Collectors.toList());
    }
}
